﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ShpRead;
using System.IO;
using System.Data.OleDb;
namespace ShpRead
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        List<PointShp> listPointShp = new List<PointShp>();    //点集
        List<LinesShp> listLineShp = new List<LinesShp>();     //线集
        List<LinesShp> listPolygonshp = new List<LinesShp>();  //多边形集
        OleDbDataAdapter myadapter;
        DataSet myds;
        ShpRead.Shp kl = new Shp();
        float LargeOrSmallRatio = 1.0f;      //坐标缩放比列
        bool flag = false;
        Point down, up;
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //点击菜单打开时执行此函数， 完成
            /*
             * 1 打开shp文件
             * 2 获取shp文件中的shp（点或线）和标注
             * 3 坐标变换，变换成屏幕坐标以后，将多边形坐标和多边形的标注保存到listPolygonshp列表中，线对象保存到
             * listLineShp中；
             * 4 坐标变换时纵横方向的变换比例要相同，不要出现拉伸或压缩图像的效果；屏幕坐标系
             *   原点位于左上角，x向右增加，y向下增加；地图坐标原点位于左下角，x向右增加，y向上增加
             * 4 把shp文件头的内容分段填写"地图概况"选项卡内
             * 5 调用FIllProptyTable函数将shp属性表显示在"属性表"内的GridView内。
             * */
            kl.OpenShpFile("D:/1/C#课设/地级区域.shp");
            textBox1.Text ="文件代码："+ kl.GetFileCode().ToString()+"\r\n";
            textBox1.Text += "文件长度：" + kl.GetFileLength().ToString() + "\r\n";
            textBox1.Text += "文件版本：" + kl.GetFileVer().ToString() + "\r\n";
            textBox1.Text += "shp类型：" + kl.GetFileType().ToString() + "\r\n";
            textBox1.Text += "x最小值：" + kl.GetXMin().ToString() + "\r\n";
            textBox1.Text += "x最大值：" + kl.GetXMax().ToString() + "\r\n";
            textBox1.Text += "y最小值：" + kl.GetYMin().ToString() + "\r\n";
            textBox1.Text += "y最大值：" + kl.GetYMax().ToString() + "\r\n";
            textBox1.Text += "z最小值：" + kl.GetZMin().ToString() + "\r\n";
            textBox1.Text += "z最大值：" + kl.GetZMax().ToString() + "\r\n";
            textBox1.Text += "m最小值：" + kl.GetMMin().ToString() + "\r\n";
            textBox1.Text += "m最大值：" + kl.GetMMax().ToString() + "\r\n";
            myds = kl.GetDataSet();  //给myds赋值
            
            object shpContent;
            int recordid, shptype;
            int a = 0;
            while (kl.GetNextShp(out shptype,out shpContent,out recordid)!=0)
            {
                
                #region 若是多边形shp
                if (shptype == 5)
                {
                    /*
                      * 
                      * 此处补充代码，完成相应功能
                      * 
                      * 
                      */
                    LinesShp ls = new LinesShp();   //定义多边形文件
                    ls = (LinesShp)shpContent;      
                    //MessageBox.Show(ls.Points.ToString());
                    float f1, f2;
                    f1 = (float)(Convert.ToDouble(pictureBox1.Width) / kl.GetXMax());  //画布宽的比例
                    f2 = (float)(Convert.ToDouble(pictureBox1.Height) / kl.GetYMax()); //画布长的比例
                    LargeOrSmallRatio = (f1 <= f2 ? f1 : f2) * 5;   //去最小值
                    //float s = pictureBox1.Width / 2;
                    //float s1 = pictureBox1.Height / 2;
                    //将每个点的坐标转成屏幕坐标
                    for (int i = 0; i <= ls.Points.Length - 1; i++)
                    {
                        ls.Points[i].X = LargeOrSmallRatio * ls.Points[i].X ;
                        ls.Points[i].Y = LargeOrSmallRatio * (float)(kl.GetYMax() - ls.Points[i].Y) ;
                    }
                    ls.label = kl.GetFieldValue(listPolygonshp.Count, 0);   //得到每个多边形的标注，注意为何用count
                    listPolygonshp.Add(ls);
                    //MessageBox.Show(ls.label);
                    
                }
                #endregion 多边形处理完成 
                
            }
            kl.CloseShpFile();
            FillProptyTable();
            DisplayShp();
        }
        //显示地图和标注
        public void DisplayShp()  
        {
            
            Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            
            Graphics g = Graphics.FromImage(bitmap);
            /*
             * 地图显示，将多边形显示到地图选项卡中，颜色和字体可以任选，也可以
             * 自定义
             * */
            Pen pen = new Pen(Color.Red, 2);
            SolidBrush sb = new SolidBrush(Color.Black);
            foreach (LinesShp ls in listPolygonshp)
            {
                /*
             * 补充代码
             * /*
             * 
             * 
             * *
             * */
                float x = 0, y = 0;
                for (int i = 0; i <= ls.NumPoints-1; i++)
                {
                    x = x + ls.Points[i].X;
                    y = y + ls.Points[i].Y;
                }

                g.DrawPolygon(pen, ls.Points);
                Font f = new Font("宋体", 9, FontStyle.Regular);
                g.DrawString(ls.label, f, sb, x/ls.NumPoints, y/ls.NumPoints);
            }
            if(pictureBox1.Image != null)
            pictureBox1.Image.Dispose();
            pictureBox1.Image = bitmap;

        }
        public void FillProptyTable()
        {
            /*
             * 填充datagridview，用来填充的数据集来自Shp的成员函数GetDataSet
             * 
             * */
            
            dataGridView1.DataSource = myds.Tables[0];
        }
        private void 属性ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Visible = !textBox1.Visible;
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 放大ToolStripMenuItem_Click(object sender, EventArgs e)   //此处放大存在bug，随着图片的放大，picturebox所占的内存逐渐变大，会存在内存不足的情况，尚未解决
        {
            float f = 1.1f;
            pictureBox1.Width = (int)(pictureBox1.Width * f);
            pictureBox1.Height = (int)(pictureBox1.Height * f);
            foreach (LinesShp ls in listPolygonshp)
            {
                for (int i = 0; i <= ls.NumPoints - 1; i++)
                {
                    ls.Points[i].X = ls.Points[i].X * f;
                    ls.Points[i].Y = ls.Points[i].Y * f;
                }
            }
            DisplayShp();
        }

        private void 缩小ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            float f = 1.1f;
            pictureBox1.Width = (int)(pictureBox1.Width / f);
            pictureBox1.Height = (int)(pictureBox1.Height / f);
            foreach (LinesShp ls in listPolygonshp)
            {
                for (int i = 0; i <= ls.NumPoints - 1; i++)
                {
                    ls.Points[i].X = ls.Points[i].X / f;
                    ls.Points[i].Y = ls.Points[i].Y / f;
                }
            }
            DisplayShp();
        }

        private void 平移ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void pictureBox1_MouseDown_1(object sender, MouseEventArgs e)
        {
            down = new Point();
            down.X = e.X;
            down.Y = e.Y;
            
        }

        private void pictureBox1_MouseUp_1(object sender, MouseEventArgs e)
        {
            up = new Point();
            up.X = e.X;
            up.Y = e.Y;
            float x = up.X - down.X;
            float y = up.Y - down.Y;
            if (flag == true)
            {
                pictureBox1.Width = (int)(pictureBox1.Width + x);
                pictureBox1.Height = (int)(pictureBox1.Height + y);
                foreach (LinesShp ls in listPolygonshp)
                {
                    for (int i = 0; i <= ls.NumPoints - 1; i++)
                    {
                        ls.Points[i].X = ls.Points[i].X + x;
                        ls.Points[i].Y = ls.Points[i].Y + y;
                    }
                }
                DisplayShp();
            }
        }

        private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            flag = true;
        }

        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            flag = false;
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            //if (flag == true)
            //{ 
            //    this.Cursor = Cursors.Cross; 
            //}
            //if (flag == false)
            //{
            //    this.Cursor = Cursors.Default;
            //}
        }
    }
}
